package com.netcracker.lab1.model;

import com.netcracker.lab1.utils.Generator;

import java.io.*;
import java.math.BigInteger;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: nocturne
 * Date: 09.01.13
 * Time: 17:39
 * To change this template use File | Settings | File Templates.
 */

class MusicTrackManager extends AbstractMusicTrackManager implements Serializable {

    private static HashMap<Integer, MusicTrack> musicTrackHashMap = new HashMap<Integer, MusicTrack>();

    @Override
    public MusicTrack createMusicTrack(Integer id, String title, String author, String album, Integer genre, int duration) throws NullPointerException{
        //MusicTrack musicTrack = new MusicTrack(Generator.getInstance().next());
        MusicTrack musicTrack = new MusicTrack(id);

        musicTrack.setId(id);
        musicTrack.setTitle(title);
        musicTrack.setAuthor(author);
        musicTrack.setAlbum(album);
        musicTrack.setGenre(genre);
        musicTrack.setDuration(duration);

        musicTrackHashMap.put(musicTrack.getId(), musicTrack);
        return musicTrack;

    }



    @Override
    public MusicTrack findTrackById(Integer id) {
        if (id >= 0) {
            return musicTrackHashMap.get(id);
        } else {
            throw new IllegalArgumentException("not found track by id = " + id);
        }
    }

    @Override
    public void deleteTrackInLibraryById(int id) {
        if (id >= 0) {
            //musicTrackHashMap.remove(id);

            Integer dId = new Integer(-1);

            for (int i = 0; i < musicTrackHashMap.size(); i++) {
                if(musicTrackHashMap.get(new Integer(i)).getId() == id) {
                    dId = i;
                    System.out.println("dId " + dId + " " + i);
                    musicTrackHashMap.remove(new Integer(i));
                }
            }

/*            if(dId != -1) {
                musicTrackHashMap.remove(new Integer(dId));
            }*/

        } else {
            throw new IllegalArgumentException("not found track by id = " + id);
        }
    }

    @Override
    public void saveMusicTrack(String path) throws FileNotFoundException, IOException {
        FileOutputStream fos = new FileOutputStream(path);
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(musicTrackHashMap);
        oos.flush();
        oos.close();
    }

    @Override
    public void openMusicTrack(String path) throws FileNotFoundException, IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(path);
        ObjectInputStream oin = new ObjectInputStream(fis);
        musicTrackHashMap = (HashMap) oin.readObject();
    }


    /* не уверен в правильности */
    @Override
    public void editMusicTrack(Integer id, String title, String author, String album, Integer genre, int duration) {
        createMusicTrack(id, title, author, album, genre, duration);
    }

    @Override
    public int getSizeLibrary() {
        return musicTrackHashMap.size();
    }

    @Override
    public MusicTrack getIdByTrackName(String author, String title) {
        for (int i = 0; i < musicTrackHashMap.size(); i++) {
            if(musicTrackHashMap.get(i).getAuthor().equals(author) && musicTrackHashMap.get(i).getAuthor().equals(author)){
                return musicTrackHashMap.get(i);
            }
        }
        return null;
    }

}
